<?php
namespace globalunit\logic;
use globalunit\utils\KeysUtil;
use QYS\Db\Mysql;
use QYS\Util\Debug;
use globalunit\utils\GenID;
use globalunit\utils\RndName;
use QYS\third\Crypto\XXTEA;
use QYS\Core\Config as CoreConfig;
use globalunit\model\AdminLogModel;
use globalunit\model\RolesModel;
use globalunit\utils\Config;
use globalunit\utils\QueueHelper;
use QYS\Log\Log;

Class AdminLogic{

    /**
     * 配置参数
     * @var array
     */
    protected static $config = [
        'id'                => 'id',        // id名称
        'pid'               => 'pid',       // pid名称
        'child'             => 'children',  // 子元素键名
        'name'              => 'name',      // 下拉列表的选项名
        'icon'              => '├',         // 下拉列表的图标
        'placeholder'       => '&nbsp;',    // 下拉列表的占位符
        'placeholder_number'=> 3,           // 下拉列表的占位符数量
    ];

    //获取权限菜单
    public static function getMenu($roles,$mysql){

        $where = 'id > 0';
        $sql = <<<crifan
            SELECT * FROM yyz_menu where $where ORDER BY sort DESC
crifan;
        $res = $mysql->query($sql);
        $list = $res->fetch_all(MYSQLI_ASSOC);
        foreach($list as $k => $v){
            $list[$k]['hidden'] = $v['hidden'] == 1 ? true : false;
            $list[$k]['alwaysShow'] = $v['alwaysShow'] == 1 ? true : false;
            $meta = json_decode($v['meta'], true);
            $meta['roles'] = explode(',', $roles);
            $list[$k]['meta'] = $meta;

        }
        $menu = self::toTree($list);
        return $menu;
    }

    //获取全部菜单
    public static function getAllMenu(){
        $mysql = Mysql::getInstance("mysql1");
        $sql = <<<crifan
            SELECT * FROM yyz_menu ORDER BY sort DESC
crifan;
        $res = $mysql->query($sql);
        $list = $res->fetch_all(MYSQLI_ASSOC);
        foreach($list as $k => $v){
            $list[$k]['hidden'] = $v['hidden'] == 1 ? true : false;
            $list[$k]['alwaysShow'] = $v['alwaysShow'] == 1 ? true : false;
            $meta = json_decode($v['meta'], true);
            if(isset($meta['roles'])){
                $meta['roles'] = explode(',', $meta['roles']);
            }
            $list[$k]['meta'] = $meta;
        }
        $menu = self::toTree($list);
        return $menu;
    }


    //创建menu菜单
    public static function toTree($data = [], $pid = 0, $limitLevel = 0, $currentLevel = 0)
    {
        $trees = [];
        $data = array_values($data);
        foreach ($data as $k => $v) {
            if ($v[self::$config['pid']] == $pid) {
                if ($limitLevel > 0 && $limitLevel == $currentLevel) {
                    return $trees;
                }
                unset($data[$k]);
                $children = self::toTree($data, $v[self::$config['id']], $limitLevel, ($currentLevel+1));
                if (!empty($children)) {
                    $v[self::$config['child']] = $children;
                }
                $trees[] = $v;
            }
        }
        return $trees;
    }


}
